package com.gztms.ordermgnt.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.gztms.enums.AppCodeEnum;
import com.gztms.ordermgnt.pojo.Order;
import com.gztms.ordermgnt.pojo.VO.OrderVO;
import com.gztms.ordermgnt.service.OrderService;
import com.gztms.utils.BeanCopyUtils;
import com.gztms.utils.ResultUtil;
import com.gztms.utils.SecurityUtils;
import com.gztms.utils.WebUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

import java.security.Principal;
import java.util.List;


/**
 * @author Zoey
 * @date 2023/4/22 21:30
 * @Description TODO
 **/
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param order
     * @return
     */
    @PostMapping("/addOrder")
    public ResultUtil addOrder(@RequestBody Order order){
        return orderService.addOrder(order);
    }

    /**
     * 支付订单
     * @param id
     * @return
     */
//    @PutMapping("/pay/{id}")
//    public ResultUtil payOrder(@PathVariable("id") Integer id) {
//        return null;
//    }

    /**
     * 删除一个订单
     * @param id
     * @return
     */
    @PostMapping("/delOrder/{id}")
    public ResultUtil delOrderById(@PathVariable("id") Long id) {
        return orderService.delOrderById(id);
    }

    /**
     * 批量删除
     * @param orderIds
     * @return
     */
    @PostMapping("/delOrders")
    public ResultUtil delOrderList(@RequestParam List<Long> orderIds) {
        return orderService.delOrderList(orderIds);
    }

    /**
     * 支付订单
     * @param id
     * @return
     */
    @PostMapping("/pay/{id}")
    public ResultUtil payOrderById(@PathVariable Long id) {
        return orderService.payOrderById(id);
    }

    /**
     * 查询订单列表
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/all")
    public ResultUtil getOrderList(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "id",required = false) Long id) {
        return orderService.getOrderList(pageNum,pageSize,status, id);
    }

    /**
     * 获取订单详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResultUtil getOrderDetail(@PathVariable("id") Long id){
        return orderService.getOrderDetail(id);
    }

//    @PostMapping("/export")
//    public void exportFile(@RequestParam(value = "ids[]",required = false) Long[] orderIds,HttpServletResponse response) {
//        System.out.println(orderIds);
//        try {
//            // 1、设置下载文件的请求头
//            WebUtils.setDownloadHeader("订单列表.xls",response);
//            // 2、获取导出数据,如果没有选择则默认导出全部
//            List<OrderVO> orderVOs = orderService.getOrderListByIds(orderIds);
//            // 3、将数据写到Excel中
//            EasyExcel.write(response.getOutputStream(),OrderVO.class).autoCloseStream(Boolean.FALSE).sheet("订单")
//                    .doWrite(orderVOs);
//
//        } catch (Exception e) {
//            response.reset();
//            ResultUtil resultUtil = ResultUtil.errorResult(AppCodeEnum.SYSTEM_ERROR);
//            WebUtils.renderString(response,JSON.toJSONString(resultUtil));
//
//        }
//
//    }

    @GetMapping("/export")
    public void exportFile(HttpServletResponse response) {
        try {
            // 1、设置下载文件的请求头
            WebUtils.setDownloadHeader("orderlist.xlsx",response);
            // 设置字体样式
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
            contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
            contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
            contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);

            WriteFont contentWriteFont = new WriteFont();
            // 字体大小
            contentWriteFont.setFontHeightInPoints((short) 12);
            contentWriteCellStyle.setWriteFont(contentWriteFont);
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            WriteFont headWriteFont = new WriteFont();
            headWriteFont.setFontHeightInPoints((short) 12);
            headWriteCellStyle.setWriteFont(headWriteFont);

            List<OrderVO> orderVOs = orderService.getAllOrders();
            // 3、将数据写到Excel中
            EasyExcel.write(response.getOutputStream(),OrderVO.class)
                    .autoCloseStream(Boolean.FALSE).sheet("订单")
                    .registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle,contentWriteCellStyle))
                    .doWrite(orderVOs);

        } catch (Exception e) {
            response.reset();
            ResultUtil resultUtil = ResultUtil.errorResult(AppCodeEnum.SYSTEM_ERROR);
            WebUtils.renderString(response,JSON.toJSONString(resultUtil));

        }

    }

    /**
     * 查询账号下所有订单
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/getUserOrders")
    public ResultUtil getOrdersByUid(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize,
            @RequestParam(value = "status", required = false) Integer status){
        String uid = SecurityUtils.getUid();
        if (uid == null) {
            return ResultUtil.errorResult(AppCodeEnum.SYSTEM_ERROR);
        }

        return orderService.getOrdersByUid(pageNum,pageSize,status,uid);
    }

}
